<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>字典管理</title>
    <!-- 引入css样式文件 -->
    <!-- Bootstrap Core CSS -->
    <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet"/>
    <!-- MetisMenu CSS -->
    <link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet"/>
    <!-- DataTables CSS -->
    <link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet"/>
    <!-- Custom CSS -->
    <link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet"/>
    <!-- Custom Fonts -->
    <link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="<%=basePath%>css/boot-crm.css" rel="stylesheet" type="text/css"/>
    <link href="<%=basePath%>css/bootstrap-datetimepicker.css" type="text/css">
</head>
<body>
<div id="wrapper">
    <jsp:include page="menu.jsp"/>
    <!-- 字典列表查询部分  start-->
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">字典管理</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form-inline" method="get"
                      action="${pageContext.request.contextPath }/baseDict/list.action">
                    <div class="form-group">
                        <label for="dict_type_code">类别名称</label>
                        <select class="form-control" id="dict_type_code" name="dict_type_code">
                            <option value="">--请选择--</option>


                            <c:forEach items="${allTypeCodeAndTypeName}" var="item">
                                <option value="${item.dict_type_code}"
                                        <c:if test="${item.dict_type_code.equals(dict_type_code)}"> selected</c:if>>
                                        ${item.dict_type_name }
                                </option>
                            </c:forEach>

                        </select>
                    </div>

                    <div class="form-group">
                        <label for="dict_item_name">项目名称</label>
                        <input type="text" class="form-control" id="dict_item_name"
                               value="${dict_item_name }" name="dict_item_name"/>
                    </div>


                    <div class="form-group">
                        <label for="dict_enable">字典状态</label>
                        <select class="form-control" id="dict_enable" name="dict_enable">
                            <option value="">--请选择--</option>

                            <option value="0"
                                    <c:if test="${dict_enable.equals('0')}"> selected</c:if>>
                                停用
                            </option>

                            <option value="1"
                                    <c:if test="${dict_enable.equals('1')}"> selected</c:if>>
                                使用
                            </option>
                        </select>
                    </div>


                    <button type="submit" class="btn btn-primary">查询</button>
                </form>
            </div>
        </div>
        <a href="#" class="btn btn-primary" data-toggle="modal"
           data-target="#newBaseDictDialog" onclick="clearBaseDict()">新建</a>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">字典信息列表</div>
                    <!-- /.panel-heading -->
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>字典id</th>
                            <th>类别代码</th>
                            <th>类别名称</th>
                            <th>项目名称</th>
                            <th>项目代码</th>
                            <th>排序字段</th>
                            <th>状态</th>
                            <th>备注</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <c:forEach items="${page.rows}" var="row">
                            <tr>
                                <td>${row.dict_id}</td>
                                <td>${row.dict_type_code}</td>
                                <td>${row.dict_type_name}</td>
                                <td>${row.dict_item_name}</td>
                                <td>${row.dict_item_code}</td>
                                <td>${row.dict_sort}</td>
                                <td>${row.dict_enable.equals('0')?"停用":"使用"}</td>
                                <td>${row.dict_memo}</td>
                                <td>
                                    <a href="#" class="btn btn-primary btn-xs" data-toggle="modal"
                                       data-target="#baseDictEditDialog"
                                       onclick="editBaseDict(${row.dict_id})">修改</a>
                                    <a href="#" class="btn btn-danger btn-xs"
                                       onclick="deleteBaseDict(${row.dict_id})">删除</a>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div class="col-md-12 text-right">
                        <ujn:page url="${pageContext.request.contextPath }/baseDict/list.action"/>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
    <!-- 字典列表查询部分  end-->
</div>
<!-- 创建字典模态框 -->
<div class="modal fade" id="newBaseDictDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新建字典信息</h4>
            </div>
            <form class="form-horizontal" id="new_baseDict_form">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="new_dict_type_code" class="col-sm-2 control-label">
                            类别代码
                        </label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" id="new_dict_type_code" placeholder="类别代码"
                                   name="dict_type_code"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="new_dict_type_name" class="col-sm-2 control-label">
                            类别名称
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_dict_type_name" placeholder="类别名称"
                                   name="dict_type_name"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_dict_item_name" class="col-sm-2 control-label">
                            项目名称
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_dict_item_name" placeholder="项目名称"
                                   name="dict_item_name"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="new_dict_item_code" class="col-sm-2 control-label">
                            项目代码
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_dict_item_code" placeholder="项目代码（可为空）"
                                   name="dict_item_code"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_dict_sort" class="col-sm-2 control-label">
                            排序字段
                        </label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" id="new_dict_sort" placeholder="排序字段"
                                   name="dict_sort"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="new_dict_enable" style="float:left;padding:7px 15px 0 27px;">字典状态</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="new_dict_enable" name="dict_enable">
                                <option value="">--请选择--</option>

                                <option value="0">
                                    停用
                                </option>
                                <option value="1">
                                    使用
                                </option>
                            </select>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="new_dict_memo" class="col-sm-2 control-label">
                            备注
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_dict_memo" placeholder="备注"
                                   name="dict_memo"/>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" onclick="createBaseDict()">创建字典</button>
        </div>
    </div>
</div>

<!-- 修改客户模态框 -->
<div class="modal fade" id="baseDictEditDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改字典信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="edit_baseDict_form">
                    <input type="hidden" id="edit_dict_id" name="dict_id"/>

                    <div class="form-group">
                        <label for="rdit_dict_type_code" class="col-sm-2 control-label">
                            类别代码
                        </label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" id="edit_dict_type_code" placeholder="类别代码"
                                   name="dict_type_code"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_dict_type_name" class="col-sm-2 control-label">
                            类别名称
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_dict_type_name" placeholder="类别名称"
                                   name="dict_type_name"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit_dict_item_name" class="col-sm-2 control-label">
                            项目名称
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_dict_item_name" placeholder="项目名称"
                                   name="dict_item_name"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="edit_dict_item_code" class="col-sm-2 control-label">
                            项目代码
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_dict_item_code" placeholder="项目代码（可为空）"
                                   name="dict_item_code"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit_dict_sort" class="col-sm-2 control-label">
                            排序字段
                        </label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" id="edit_dict_sort" placeholder="排序字段"
                                   name="dict_sort"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="edit_dict_enable" style="float:left;padding:7px 15px 0 27px;">字典状态</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="edit_dict_enable" name="dict_enable">
                                <option value="">--请选择--</option>

                                <option value="0">
                                    停用
                                </option>
                                <option value="1">
                                    使用
                                </option>
                            </select>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="edit_dict_memo" class="col-sm-2 control-label">
                            备注
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_dict_memo" placeholder="备注"
                                   name="dict_memo"/>
                        </div>
                    </div>


                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="updateBaseDict()">保存修改</button>
            </div>
        </div>
    </div>
</div>
<!-- 引入js文件 -->
<!-- jQuery -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
<script src="<%=basePath%>js/moment.min.js"></script>
<script src="<%=basePath%>js/bootstrap-datetimepicker.min.js"></script>
<!-- 编写js代码 -->
<script type="text/javascript">

    //清空新建字典窗口中的数据
    function clearBaseDict() {
        $("#new_dict_type_code").val("");
        $("#new_dict_type_name").val("")
        $("#new_dict_item_name").val("")
        $("#new_dict_item_code").val("");
        $("#new_dict_sort").val("");
        $("#new_dict_enable").val("");
        $("#new_dict_memo").val("");
    }

    // 创建
    function createBaseDict() {
        $.post("<%=basePath%>baseDict/create.action",
            $("#new_baseDict_form").serialize(), function (data) {
                if (data == "OK") {
                    alert("字典创建成功！");
                    window.location.reload();
                } else {
                    alert("未知错误,创建失败");
                    window.location.reload();
                }
            });
    }
    
    // 通过id获取修改的信息
    function editBaseDict(id) {
        $.ajax({
            type: "get",
            url: "<%=basePath%>baseDict/getBaseDictById.action",
            data: {"id": id},
            success: function (data) {
                $("#edit_dict_id").val(data.dict_id);
                $("#edit_dict_type_code").val(data.dict_type_code);
                $("#edit_dict_type_name").val(data.dict_type_name);
                $("#edit_dict_item_name").val(data.dict_item_name)
                $("#edit_dict_item_code").val(data.dict_item_code)
                $("#edit_dict_sort").val(data.dict_sort)
                $("#edit_dict_enable").val(data.dict_enable)
                $("#edit_dict_memo").val(data.dict_memo)
            }
        });
    }

    // 执行修改操作
    function updateBaseDict() {
        $.post("<%=basePath%>baseDict/update.action", $("#edit_baseDict_form").serialize(), function (data) {
            if (data == "OK") {
                alert("字典修改成功！");
                window.location.reload();
            } else {
                alert("未知错误,修改失败");
                window.location.reload();
            }
        });
    }

    // 删除
    function deleteBaseDict(id) {
        if (confirm('确实要删除该字典吗?可能会造成系统灾难性损坏！')) {
            $.post("<%=basePath%>baseDict/delete.action", {"id": id},
                function (data) {
                    if (data == "OK") {
                        alert("字典删除成功！");
                        window.location.reload();
                    } else {
                        alert("删除字典失败！");
                        window.location.reload();
                    }
                });
        }
    }
    
</script>
</body>
</html>